import { queryList } from '@/api/api.js'
import { regiongetAll } from '@/api/lcmapi.js'
export default {
    namespaced: true,
    state: {
        // 页面文档可视高度(随窗口改变大小)
        documentClientHeight: 0,
        // 导航条, 布局风格, defalut(默认) / inverse(反向)
        navbarLayoutType: 'defalut',
        // 侧边栏, 布局皮肤, light(浅色) / dark(黑色)
        sidebarLayoutSkin: 'light',
        // 侧边栏, 折叠状态
        sidebarFold: false,
        // 侧边栏, 菜单
        menuList: [],
        menuActiveName: '',
        // 内容, 是否需要刷新
        contentIsNeedRefresh: false,
        // 主入口标签页
        mainTabs: [],
        mainTabsActiveName: '',
        regions: []
    },
    mutations: {
        updateDocumentClientHeight(state, height) {
            state.documentClientHeight = height
        },
        updateNavbarLayoutType(state, type) {
            state.navbarLayoutType = type
        },
        updateSidebarLayoutSkin(state, skin) {
            state.sidebarLayoutSkin = skin
        },
        updateSidebarFold(state, fold) {
            state.sidebarFold = fold
        },
        updateMenuList(state, list) {
            state.menuList = list
        },
        updateMenuActiveName(state, name) {
            state.menuActiveName = name
        },
        updateContentIsNeedRefresh(state, status) {
            state.contentIsNeedRefresh = status
        },
        updateMainTabs(state, tabs) {
            state.mainTabs = tabs
        },
        updateMainTabsActiveName(state, name) {
            state.mainTabsActiveName = name
        },
        updateRegions(state, regions) {
            if (regions) {
                state.regions = regions
            }
        }
    },
    actions: {
        async saveRegion({ commit }) {
            let res = await regiongetAll({})
            if (res[0]) { return }
            let data = res[1]
            if (data && data.code === 0) {
                let regions = data.data.filter(ele => ele.level === 1).map(ele => {
                    return { label: ele.name, value: ele.id }
                })
                regions.forEach(province => {
                    let citys = []
                    citys = data.data.filter(ele => ele.level === 2 && province.value === ele.pid).map(city => {
                        return { label: city.name, value: city.id } || []
                    })
                    if (citys && citys.length) {
                        citys.forEach(city => {
                            let districts = data.data.filter(ele => ele.level === 3 && city.value === ele.pid).map(district => {
                                return { label: district.name, value: district.id } || []
                            })
                            city.children = districts
                        })
                    }
                    province.children = citys
                })
                commit('updateRegions', regions)
            }
        }
    }
}